﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using WickedFlame.Core.State;
using System.Windows.Media.Imaging;

namespace WickedFlame.Core.Shapes
{
	public class EllipseShape : RectangleBase, IStateCapturable, ICopyable
	{
        public EllipseShape()
            : base(0, 0, 1, 1)
        {
        }

        public EllipseShape(double x, double y, double width, double height) :
            base(x, y, width, height)
        {
        }
        
		private EllipseGeometry DefiningGeometry
		{
			get
			{
				var definingGeometry = new System.Windows.Media.EllipseGeometry();

                definingGeometry.Center = new Point(
                    Rectangle.X + (Rectangle.Width / 2),
                    Rectangle.Y + (Rectangle.Height / 2)
                    );

                definingGeometry.RadiusX = Rectangle.Width / 2;
                definingGeometry.RadiusY = Rectangle.Height / 2;

				return definingGeometry;
			}
		}

        public override void Draw(DrawingContext drawingContext)
        {
            double centerX = Rectangle.X + (Rectangle.Width / 2);
            double centerY = Rectangle.Y + (Rectangle.Height / 2);

            // rotate the shape
            if (Rotation > 0)
                drawingContext.PushTransform(new RotateTransform(Rotation, centerX, centerY));

            drawingContext.DrawEllipse(
                Fill,
                Pen /*new Pen(Stroke, StrokeThickness)*/,
                new Point(centerX, centerY),
                Rectangle.Width / 2,
                Rectangle.Height / 2);
        }

        #region Properties

        public double Width
        {
            get
            {
                return Rectangle.Width;
            }
            set
            {
                Rectangle = new Rect(Rectangle.X, Rectangle.Y, value, Rectangle.Height);
				Invalidate();
            }
        }

        public double Height
        {
            get
            {
                return Rectangle.Height;
            }
            set
            {
                Rectangle = new Rect(Rectangle.X, Rectangle.Y, Rectangle.Width, value);
				Invalidate();
            }
        }

        public double X
        {
            get
            {
                return Rectangle.X;
            }
            set
            {
                Rectangle = new Rect(value, Rectangle.Y, Rectangle.Width, Rectangle.Height);
				Invalidate();
            }
        }

        public double Y
        {
            get
            {
                return Rectangle.Y;
            }
            set
            {
                Rectangle = new Rect(Rectangle.X, value, Rectangle.Width, Rectangle.Height);
				Invalidate();
            }
        }

        #endregion

		public override void Invalidate()
		{
			base.Invalidate();

			NotifyPropertyChanged("Width");
			NotifyPropertyChanged("Height");
			NotifyPropertyChanged("X");
			NotifyPropertyChanged("Y");

			DrawingCanvas.DrawTrackers();
		}

		protected override bool PointInObject(Point point)
		{
            //Rect _rectangle = _definingGeometry.Bounds;
            return DefiningGeometry.FillContains(point);
            //return baseRectangle.Contains(point);
		}

        #region IHistoryable Members

        public ShapeMemento CreateMementoState()
        {
            var memento = new EllipseMemento();
            memento.SaveProperties(this);
            return memento;
        }

        public void ResetState(ShapeMemento memento)
        {
            memento.ResetProperties(this);
            Invalidate();
        }

        #endregion

        #region ICopyable Members

        public VectorShape Copy()
        {
            var shape = new EllipseShape(Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height)
            {
                Fill = Fill,
                Stroke = Stroke,
                StrokeThickness = StrokeThickness
            };

            return shape;
        }

        #endregion
    }
}
